package com.example.shoppingdemo.nslcommodity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.shoppingdemo.MainActivity;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.goods.goodsActivity;
import com.example.shoppingdemo.nslcommodity.IResult.CommodityRs;
import com.example.shoppingdemo.nslcommodity.IResult.ICommodityResult;
import com.example.shoppingdemo.nslcommodity.adapter.BrandAdapter;
import com.example.shoppingdemo.nslcommodity.javabean.BrandModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品列表页面
 */
public class CommodityList extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private RecyclerView commoditylist;
    private RecyclerView brandRecycler;
    private List<CommodityModel> commodityModelList;
    private List<BrandModel> brandModels;
    private CommodityAdapter adapter;
    private BrandAdapter brandAdapter;
    private Context mContext;
    int goodsType=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_list);
        initView();
    }

    /**
     * 具体操作
     */
    private void initView(){
        /**
         *添加返回按钮点击事件
         */
        ImageView back=findViewById(R.id.image_list_fanhui);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        /**
         * 初始化适配器，给RecyclerView附加适配器
         */
        brandRecycler=findViewById(R.id.recycler_brand);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        brandRecycler.setLayoutManager(layoutManager);
        brandModels=new ArrayList<>();
        brandAdapter=new BrandAdapter(this);
        brandAdapter.setBrandModels(brandModels);
        brandAdapter.setOnBrandParticulars(new BrandAdapter.onBrandParticulars() {
            @Override
            public void onBrandResult(int position) {

            }
        });
        brandRecycler.setAdapter(brandAdapter);
        commodityModelList=new ArrayList<>();
        commoditylist=findViewById(R.id.Recycler_commodity_list);
        adapter=new CommodityAdapter(this);
        commoditylist.setLayoutManager(new LinearLayoutManager(this));
        adapter.setCommodityModels(commodityModelList);
        ICommodityResult commodityResult=new CommodityRs(this);;
        commodityResult.onShopping(1,new CommodityRs.onShopResponse() {
            @Override
            public void onShopList(List<CommodityModel> commodityModels) {
                commodityModelList.clear();
                adapter.setCommodityModels(commodityModels);
                adapter.notifyDataSetChanged();
            }
        });
        commoditylist.setAdapter(adapter);
        /**
         *给图片加点击事件，能够切换布局
         */
        ImageView arrange=findViewById(R.id.image_pailie);
        arrange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (goodsType==0){
                    adapter.setType(1);
                    /**
                     * 网格布局
                     */
                    arrange.setImageResource(R.drawable.ic_pailie_24);
                    commoditylist.setLayoutManager(new StaggeredGridLayoutManager(
                            2,StaggeredGridLayoutManager.VERTICAL));
                    adapter.notifyDataSetChanged();
                    goodsType=1;
                }else {
                    adapter.setType(0);
                    /**
                     * 垂直布局
                     */
                    arrange.setImageResource(R.drawable.ic_peilie1_24);
                    commoditylist.setLayoutManager(new LinearLayoutManager(mContext));
                    adapter.notifyDataSetChanged();
                    goodsType=0;
                }
            }
        });
        /**
         * 给adapter设置监听，点击跳转
         */
        adapter.setCommodityParticulars(new CommodityAdapter.onCommodityParticulars() {
            @Override
            public void onCommodityResult(int position) {
                Intent intent=new Intent(CommodityList.this,goodsActivity.class);
                startActivity(intent);
            }
        });
    }
}